/**
 * JAVACC DEMO 1.0
 * @Package com.apache.passport.service.plugins
 */
package com.apache.passport.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.datasource.DynamicDataSourceHolder;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.passport.common.ConversionModelFactory;
import com.apache.passport.common.SsoConversionModel;
import com.apache.passport.entity.UctUser;
import com.apache.tools.DataMap;

/**
 * description:  验证邮箱是否唯一
 */
public class ChkEmailPlugin extends SuperPluginConnector {
//	protected IDao uctUserDao;
//
//	public void setUctUserDao(IDao uctUserDao) {
//		this.uctUserDao = uctUserDao;
//	}

	/**
	 * TODO 校验邮箱唯一性.  
	 * @see PluginConnector#execute(ParamsVo)
	 */
	@Override
	public Object execute(ParamsVo paramsvo) throws Exception {
		ResultEntity result = new ResultEntity();
		result.setResult("true");
		result.setEntity("F");
		result.setMessage("数据查询异常");
		//判断邮箱是否为空
		String email = (String) paramsvo.getParams("email");
		if (Validator.isNull(email)) {
			result.setEntity("F");
			result.setMessage("邮箱不能为空");
			return result;
		}
		UctUser uctUser = (UctUser) getInfoByEmail(email);
		if (Validator.isEmpty(uctUser)) {
			result.setEntity("T");
			result.setMessage("邮箱不存在");
			return result;
		} else {
			result.setMessage("邮箱已存在");
		}
		return result;
	}

	/**
	 * description:  根据邮箱查询用户信息
	 * @return
	 * @author Hou Dayu 
	 * @param email 
	 * @update 2014-12-19
	 */
	public Object getInfoByEmail(String email) {
		String sql = uctUserDao.getSql(4);
		MethodParam param = new MethodParam("ByEmail", "", sql, "com.apache.passport.entity.UctUser");
		if(ConversionModelFactory.getInstance().isEnable()){
			SsoConversionModel model = ConversionModelFactory.getInstance().getModel();
			sql = model.sqlByEmail(email);
			DynamicDataSourceHolder.putDataSource(ConversionModelFactory.USER_DB_KEY);
			param.setParams("dyncSql", sql);
			param.setKey("ByObjInfo");
			DataMap<String> map = (DataMap)uctUserDao.selectSingle(param);
			return getUctUser(map);
		}else {
			param.setParams("email", email);
			return uctUserDao.selectSingle(param);
		}
	}


}
